<!DOCTYPE html>
<html>
<head>
    <title>备件耗用记录</title>
    #parse("sys/header.html")
</head>
<body>
<div id="rrapp" v-cloak>
    <div v-show="showList">
        <Row :gutter="16" style="margin: 8px 0px 0px 1px">
            <div class="buttons-group">
                <i-button type="info" @click="getSparePart"><i class="fa fa-plus"></i>&nbsp;选择备件</i-button>
                <i-button type="error" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</i-button>
            </div>
        </Row>
        <table id="jqGrid"></table>
    </div>
</div>

<script>
    $(function () {
        var queryUrl = '../sparepartconsumrecord/list';
        var paramArray = [];
        var businessId = getQueryString("businessId");
        if (businessId) {
            vm.businessId = businessId;
            paramArray.push('businessId=' + businessId);
        }
        type = getQueryString("type");
        if (type) {
            vm.type = type;
            paramArray.push('type=' + type);
        }
        var materialType = getQueryString('materialType');
        if (materialType) {
            vm.materialType = materialType;
            paramArray.push('materialType=' + materialType);
        }
        // 设备类型 1生产设备 2 能源设备
        devKind = getQueryString("devKind");
        if (devKind) {
            vm.devKind = devKind;
        } else {
            // 没传递默认为普通设备
            vm.devKind = '1';
        }
        if (paramArray.length > 0) {
            queryUrl += '?' + paramArray.join('&');
        }
        $.jgrid && ($.jgrid.info_dialog = function (caption, content, c_b, modalopt) {
            alert(content);
        });
        $("#jqGrid").Grid({
            url: queryUrl,
            colModel: [
                {label: '备件编码', name: 'partCode', index: 'PART_CODE', width: 80},
                {label: '备件名称', name: 'name', index: 'NAME', width: 120},
                {label: '规格', name: 'spec', index: 'SPEC'},
                {label: '使用数量', name: 'num', index: 'NUM', width: 60, editable: true, editrules: {number: true}},
                {label: '添加人', name: 'createUserName', width: 60, sortable: false},
                {label: '添加时间', name: 'gmtCreate', width: 60, sortable: false},
                {label: '领料单号', name: 'materCode', width: 60, sortable: false},
                {name: 'type', hidden: true},
                {name: 'businessId', hidden: true},
                {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            ],
            forceFit: true,
            cellEdit: true,
            cellsubmit: 'remote',
            cellurl: '../sparepartconsumrecord/update',
            afterSubmitCell(serverresponse, rowid, cellname, value, iRow, iCol) {
                vm.reload();
                if (serverresponse.responseJSON.code == 0) {
                    vm.reload();
                    return [true,""];
                } else {
                    return [false,serverresponse.responseJSON.msg];
                }
            }
        });
    });

    var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            materialType:'',//工单类别
            devKind: '', //设备类型
            businessId: '', // 一般为维修记录或者维护工单ID
            type: '', // 耗用类别
            name: '', // 备件名称
            spec: '',// 规格
            sparePartConsumRecord: {
                num: '',
                sparePartId: '',
                businessId: '',
                type: '',
                id: '',
                materialType:''
            }
        },
        methods: {
            //显示备件列表-物料编码
            getSparePart: function () {
                let _this = this;

                if (vm.devKind == '1') {
                    // 生产设备 sparePartmaintenanceDetails
                    openWindow({
                        type: 2,
                        title: '<i class="fa fa-file-text-o"></i> 选择备件',
                        // content: '../spare/sparePartQuery.html',
                        content: '../spare/sparePartmaintenanceDetails.html?materialType='+_this.materialType,
                        btn: ['确定', '取消'],
                        btn1: function (index) {
                            var iframeWin = window["layui-layer-iframe" + index];
                            var res = iframeWin.getSparePart();
                            if (res.actualNum - res.replaceNum < 0) {
                                alert('可用数据小于1');
                                return
                            }
                            vm.sparePartConsumRecord = {
                                num: 0,
                                materialRecordId: res.id, // 领料明细ID
                                businessId: _this.businessId, // 维修记录ID
                                type: _this.type,
                                materialType:_this.type,
                            };
                            layer.close(index);
                            Ajax.request({
                                url: "../sparepartconsumrecord/save",
                                params: JSON.stringify(vm.sparePartConsumRecord),
                                type: "POST",
                                contentType: "application/json",
                                successCallback: function (r) {
                                    alert('操作成功', function (index) {
                                        vm.reload();
                                    });
                                }
                            });
                        }
                    })
                } else if (vm.devKind == '2') {
                    // 能源设备从备件库存中选择
                    openWindow({
                        type: 2,
                        title: '<i class="fa fa-file-text-o"></i> 选择备件',
                        content: '../spare/sparestock_choose.html',
                        btn: ['确定', '取消'],
                        btn1: function (index) {
                            var iframeWin = window["layui-layer-iframe" + index];
                            var res = iframeWin.getSpareStroe();
                            if (!res) {
                                iview.Message.error("请选择一条记录");
                            } else {
                                let storeNum = parseInt(res.storeNum);
                                if (storeNum == 0 || (storeNum - parseInt(res.locNum) <= 0)) {
                                    alert("可用库存不足，请更换!");
                                    return;
                                }
                                vm.sparePartConsumRecord = {
                                    num: 0,
                                    sparePartId: res.spareId,
                                    businessId: _this.businessId,
                                    type: _this.type,
                                    posId: res.id,
                                    materialType:_this.type,
                                };
                                layer.close(index);
                                Ajax.request({
                                    url: "../sparepartconsumrecord/save",
                                    params: JSON.stringify(vm.sparePartConsumRecord),
                                    type: "POST",
                                    contentType: "application/json",
                                    successCallback: function (r) {
                                        alert('操作成功', function (index) {
                                            vm.reload();
                                        });
                                    }
                                });
                            }
                        }
                    })
                }
            },
            query: function () {
                vm.reload();
            },
            del: function (event) {
                let ids = getSelectedRows("#jqGrid");
                if (ids == null) {
                    return;
                }

                confirm('确定要删除选中的记录？', function () {
                    Ajax.request({
                        url: "../sparepartconsumrecord/delete",
                        params: JSON.stringify(ids),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function () {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                });
            },
            reload: function (event) {
                vm.showList = true;
                let page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {},
                    page: page
                }).trigger("reloadGrid");
            }
        }
    });
</script>
</body>
</html>
